﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using HIWSystem.Common;
using HIWSystem.Models;

namespace HIWSystem.ViewModel
{
    [MetadataType(typeof(MenuNamedViewModel))]
    public class MenuNamedViewModel : ViewModelBase ,IDataErrorInfo
    {
        #region 属性
        /// <summary>
        /// 表单验证错误集合
        /// </summary>
        private Dictionary<String, String> dataErrors = new Dictionary<String, String>();
        private string menuCn="";
        /// <summary>
        /// 菜单中文名
        /// </summary>
        [Required]
        public string MenuCn
        {
            get { return menuCn; }
            set { menuCn = value; this.RaisePropertyChanged(() => MenuCn); }
        }
        private string menuEn="";
        /// <summary>
        /// 菜单的英文名
        /// </summary>
        [RegularExpression(@"^[a-zA-Z]+$",ErrorMessage ="英文取值范围[a-zA-Z]")]
        public string MenuEn
        {
            get { return menuEn; }
            set { menuEn = value; this.RaisePropertyChanged(() => MenuEn); }
        }
        private string remarks="";
        /// <summary>
        /// 备注
        /// </summary>
        public string Remarks
        {
            get { return remarks; }
            set { remarks = value; this.RaisePropertyChanged(() => Remarks); }
        }
        private ObservableCollection<MenuNamedEntity> menuResource;
        /// <summary>
        /// 菜单资源
        /// </summary>
        public ObservableCollection<MenuNamedEntity> MenuResource
        {
            get { return menuResource; }
            set { menuResource = value; this.RaisePropertyChanged(() => MenuResource); }
        }

        #endregion

        #region 命令
        private RelayCommand submitCmd;

        public RelayCommand SubmitCmd
        {
            get 
            {
                if (submitCmd==null)
                {
                    submitCmd = new RelayCommand(Submit);
                }
                return submitCmd;
            }
            set { submitCmd = value; }
        }
        private RelayCommand loadPageCmd;

        public RelayCommand LoadPageCmd
        {
            get 
            {
                if (loadPageCmd==null)
                {
                    loadPageCmd = new RelayCommand(LoadedPage);
                }
                return loadPageCmd; 
            }
            set { loadPageCmd = value; }
        }

        public string Error
        {
            get
            {
                return null;
            }
        }

        public string this[string columnName]
        {
            get
            {
                ValidationContext vc = new ValidationContext(this, null, null);
                vc.MemberName = columnName;
                var res = new List<ValidationResult>();
                var result = Validator.TryValidateProperty(this.GetType().GetProperty(columnName).GetValue(this, null), vc, res);
                if (res.Count > 0)
                {
                    AddDic(dataErrors, vc.MemberName);
                    return string.Join(Environment.NewLine, res.Select(r => r.ErrorMessage).ToArray());
                }
                RemoveDic(dataErrors, vc.MemberName);
                return null;
            }
        }

        #endregion
        #region 方法
        /// <summary>
        /// 添加命名菜单信息
        /// </summary>
        private void Submit()
        {
            if (dataErrors.Count == 0)
            {
                bool result = false;
                DataTable dtMenu = new DataTable();
                MenuNamedEntity menuNamedEntity = new MenuNamedEntity()
                {
                    MenuCn = this.MenuCn ?? "",
                    MenuEn = this.MenuEn ?? "",
                    Remarks = this.Remarks ?? "",

                };
                result = BusinessProcess.MenuDefine(menuNamedEntity);
                if (result)
                {
                    dtMenu = BusinessProcess.GetMenuDefineTable();
                    MenuResource = new ObservableCollection<MenuNamedEntity>();
                    for (int i = 0; i < dtMenu.Rows.Count; i++)
                    {
                        MenuResource.Add(
                            new MenuNamedEntity
                            {
                                MenuCn = dtMenu.Rows[i]["MenuCn"].ToString(),
                                MenuEn = dtMenu.Rows[i]["MenuEn"].ToString(),
                                Remarks = dtMenu.Rows[i]["Remarks"].ToString(),
                            });
                    }
                    this.MenuCn = "";
                    this.MenuEn = "";
                    this.Remarks = "";
                    dtMenu = new DataTable();
                    dtMenu = BusinessProcess.GetNoConfigMenuTable();
                    Messenger.Default.Send<DataTable>(dtMenu, "NewAddMenu");
                    MessageBox.Show("提交成功!", "软件温馨提示!", MessageBoxButton.OK, MessageBoxImage.Information);

                }
                else //操作失败
                {
                    MessageBox.Show("提交失败!请检查是否重复添加?", "软件温馨提示!", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            } /*MessageBox.Show("验证通过！");*/
            else
                MessageBox.Show("信息输入有误！", "软件温馨提示!", MessageBoxButton.OK, MessageBoxImage.Error);
        }
        /// <summary>
        /// 页面加载
        /// </summary>
        private void LoadedPage()
        {
            DataTable dtMenu = new DataTable();
            dtMenu = BusinessProcess.GetMenuDefineTable();
            MenuResource = new ObservableCollection<MenuNamedEntity>();
            for (int i = 0; i < dtMenu.Rows.Count; i++)
            {
                MenuResource.Add(
                    new MenuNamedEntity
                    {
                        MenuCn = dtMenu.Rows[i]["MenuCn"].ToString(),
                        MenuEn = dtMenu.Rows[i]["MenuEn"].ToString(),
                        Remarks = dtMenu.Rows[i]["Remarks"].ToString(),
                    });
            }
        }
        /// <summary>
        /// 移除字典
        /// </summary>
        /// <param name="dics"></param>
        /// <param name="dicKey"></param>
        private void RemoveDic(Dictionary<String, String> dics, String dicKey)
        {
            dics.Remove(dicKey);
        }

        /// <summary>
        /// 添加字典
        /// </summary>
        /// <param name="dics"></param>
        /// <param name="dicKey"></param>
        private void AddDic(Dictionary<String, String> dics, String dicKey)
        {
            if (!dics.ContainsKey(dicKey)) dics.Add(dicKey, "");
        }
        #endregion
    }
}
